package com.jak.demo.test;


import java.util.concurrent.TimeUnit;

class MyData {
    volatile int number = 0;

    public void addTo60() {
        this.number = 60;
    }
}

/**
 * 1 验证volatile的可见性
 *  1.1 假如int number = 0; number变量之前根本没有添加volatile关键字修饰
 */
public class VolatileDemo {

    // main是一切方法的运行入口
    public static void main(String[] args) {
        // 资源类
        MyData myData = new MyData();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in");
            //暂停一会儿线程
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            myData.addTo60();
            System.out.println(Thread.currentThread().getName() + "\t updated number value: " + myData.number);
        }, "AAA").start();
        //第2个线程就是我们的main线程
        while(myData.number == 0) {
            // main线程就一直在这里等待循环，直到number值不再等于零。
        }
        System.out.println(Thread.currentThread().getName() + "\t mission is over, main get number value: " + myData.number);
    }

}
